<?php
class CategoryController extends CController {
	public $defaultAction = 'view';
	
	public function actionView() {
		if(isset($_GET['id'])) {
			$categoryId = (int)$_GET['id'];
			$criteria = new CDbCriteria;
			$criteria->condition = 'termTaxonomyId = '.$categoryId.' AND taxonomy = "'.TermsTaxonomy::TAXONOMY_CATEGORY.'"';
			$category = TermsTaxonomy::model()->find($criteria);
			if($category !== null) {
				$criteria = new CDbCriteria;
				$criteria->order = 'postId DESC';
				$criteria->condition = 'termTaxonomyId = '.$categoryId.' AND taxonomy = "'.TermsTaxonomy::TAXONOMY_CATEGORY.'"';
				$with = array('categories');
				// Get the count of all posts
				$postsCount = Posts::model()->published()->with($with)->count($criteria);
				// Pagination
				$pages = new CPagination($postsCount);
				$pages->pageSize = 10;
				$pages->applyLimit($criteria);
				// Posts' data
				$posts = Posts::model()->published()->with($with)->findAll($criteria);
				
				$this->setPageTitle($category->terms->name.' - 广州心理咨询网');
				$this->render('view', array(
						'posts' => $posts,
						'pages' => $pages,
						'category' => $category
					)
				);
			}
		}
	}
}